package cn.silver.module.monitor.service.impl;

import cn.silver.framework.core.utils.MapstructUtils;
import cn.silver.framework.log.event.OperEvent;
import cn.silver.framework.mybatis.service.impl.BaseServiceImpl;
import cn.silver.module.monitor.domain.LogOper;
import cn.silver.module.monitor.mapper.LogOperMapper;
import cn.silver.module.monitor.bean.LogOperBean;
import cn.silver.module.monitor.param.OperParam;
import cn.silver.module.monitor.service.ILogOperService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 操作日志 服务层处理
 *
 * @author Silver Zhou
 */
@Service
@RequiredArgsConstructor
public class LogOperServiceImpl extends BaseServiceImpl<LogOperMapper, LogOper, OperParam, LogOperBean> implements ILogOperService {


    /**
     * 操作日志记录
     *
     * @param operEvent 操作日志事件
     */
    @Async
    @EventListener
    public void recordOper(OperEvent operEvent) {
        LogOperBean operLog = MapstructUtils.convert(operEvent, LogOperBean.class);
        // 远程查询操作地点
        this.insert(operLog);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        baseMapper.delete(new LambdaQueryWrapper<>());
    }
}
